/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import businessObjects.Esquema;
import businessObjects.Estado;
import businessObjects.Pauta;
import daos.EsquemaDAO;
import daos.EstadoDAO;
import daos.PautaDAO;
import daos.exceptions.IllegalOrphanException;
import daos.exceptions.NonexistentEntityException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author williamprado
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class PautaServices implements PautaServicesLocal {

    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    @PostConstruct
    public void initialize(){
    entityManager = entityManagerFactory.createEntityManager();
    }

    @PreDestroy
    public void cleanUp(){
        if(entityManager.isOpen()){
            entityManager.close();
        }
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public void nuevaPauta(Pauta pauta) {
        PautaDAO pd = new PautaDAO();
        try {
            pd.create(pauta, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Pauta consultarPauta(int id) {
        PautaDAO pd = new PautaDAO();
        Pauta pauta = new Pauta();
        try {
            pauta = pd.findPauta(id, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pauta;
    }

    public void actualizarPauta(Pauta pauta) {
        PautaDAO pd = new PautaDAO();
        try {
            pd.edit(pauta, entityManager);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void eliminarPauta(int id) {
        PautaDAO pd = new PautaDAO();
        try {
            pd.destroy(id, entityManager);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    public Esquema obtenerEsquema(int id){
        EsquemaDAO ed = new EsquemaDAO();
        Esquema esq = new Esquema();
        try {
            esq = ed.findEsquema(id, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return esq;
    }

    public Estado obtenerEstado(int id){
        EstadoDAO ed = new EstadoDAO();
        Estado es = new Estado();
        try {
            es = ed.findEstado(id, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return es;
    }

    public List<Pauta> pautasCliente(String id) {
        try {
            PautaDAO pd = new PautaDAO();
            List<Pauta> r = null;
            List<Pauta> pb = pd.findPautaEntities(entityManager);
            if(pb.isEmpty()){
                return pb;
            }else{
                for (int i = 0; i < pb.size(); i++) {
                    if (pb.get(i).getLoginCliente().getLogin().equals(id)) {
                        r.add(pb.get(i));
                    }
                }
                return r;
            }
        }
        catch (Exception ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public List<Pauta> pautasActivas(List<Pauta> pautas) {
        Date hoy = new Date();
        List<Pauta> lp = null;
        if(pautas.isEmpty()){
            return pautas;
        }else{
            for(int i=0; i<pautas.size(); i++){
                if(pautas.get(i).getFechaInicio().equals(hoy) || pautas.get(i).getFechaInicio().before(hoy)){
                    if(pautas.get(i).getFechaFin().equals(hoy) || pautas.get(i).getFechaFin().after(hoy)){
                        lp.add(pautas.get(i));
                    }
                }
            }
            return lp;
        }
    }

    public List<Pauta> pautasBasicas() {
        try {
            PautaDAO pd = new PautaDAO();
            int id = 1;
            List<Pauta> pb = null;
            List<Pauta> p = pd.findPautaEntities(entityManager);
            if(p.isEmpty()){
                return p;
            }else{
                for (int i = 0; i < p.size(); i++) {
                    if(p.get(i).getIdEsquema().getIdEsquema() == id){
                        pb.add(p.get(i));
                    }
                }
                List<Pauta> pa = pautasActivas(pb);
                return pa;
            }
        }
        catch (Exception ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }        
    }

    public List<Pauta> pautasPremium() {
        try {
            PautaDAO pd = new PautaDAO();
            int id = 2;
            List<Pauta> pp = null;
            List<Pauta> p = pd.findPautaEntities(entityManager);
            if(p.isEmpty()){
                return p;
            }else{
                for (int i = 0; i < p.size(); i++) {
                    if (p.get(i).getIdEsquema().getIdEsquema() == id){
                        pp.add(p.get(i));
                    }
                }
                List<Pauta> pa = pautasActivas(pp);
                return pa;
            }
        }
        catch (Exception ex) {
            Logger.getLogger(PautaServices.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }        
    }   
 
}
